<template>
  <div class="app-container">
    <el-row :gutter="20">
      <splitpanes :horizontal="this.$store.getters.device === 'mobile'" class="default-theme">
        <!--用户数据-->
        <pane size="84">
          <el-col>
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
              label-width="68px">
              <el-form-item label="手机号码" prop="phoneNumber">
                <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号码" clearable style="width: 240px"
                  @keyup.enter.native="handleQuery" />
              </el-form-item>

              <el-form-item label-width="120px" label="时间" prop="daterange">
                <el-date-picker v-model="dateRange" type="daterange" value-format="yyyy-MM-dd" range-separator="至"
                  start-placeholder="开始日期" end-placeholder="结束日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5"></el-col>
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table v-loading="loading" :data="userList">
              <el-table-column type="selection" width="50" align="center" />
              <el-table-column label="姓名" align="center" prop="userName" width="120" />
              <el-table-column label="手机号码" align="center" prop="phoneNumber" width="120" />
              <el-table-column label="创建时间" align="center" prop="createTime" width="160">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="结算状态" align="center" prop="status" min-width="120">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.status == '1'" type="success">已结算</el-tag>
                  <el-tag v-else type="danger">待结算</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="结算时间" align="center" prop="settlementDate" width="200" />
              <el-table-column label="结算金额" align="center" prop="settlementAmount" width="120"
                :show-overflow-tooltip="true" />
              <el-table-column label="注册用户数" align="center" prop="registerNum" width="120"
                :show-overflow-tooltip="true" />
              <el-table-column label="会员用户数" align="center" prop="vipNum" width="120" :show-overflow-tooltip="true" />
              <el-table-column label="基数比例" align="center" prop="cardinality" width="120" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                  {{ scope.row.cardinality || 0 }}%
                </template>
              </el-table-column>
              <el-table-column label="封顶比例" align="center" prop="capping" width="120" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                  {{ scope.row.capping || 0 }}%
                </template>
              </el-table-column>
              <el-table-column label="增长比例" align="center" prop="increase" width="160">
                <template slot-scope="scope">
                  {{ scope.row.increase || 0 }}%
                </template>
              </el-table-column>
              <el-table-column label="最少人数基数" align="center" prop="minPeopleNum" width="160"></el-table-column>
              <el-table-column label="结算比例" align="center" prop="settlementProportion" width="160">
                <template slot-scope="scope">
                  {{ scope.row.settlementProportion || 0 }}%
                </template>
              </el-table-column>
              <el-table-column label="结算人" align="center" prop="createBy" width="160"></el-table-column>

              <el-table-column label="操作" align="center" fixed="right" width="160"
                class-name="small-padding fixed-width">
                <template slot-scope="scope" v-if="scope.row.userId !== 1 && scope.row.status != 1">
                  <el-button size="mini" type="text" icon="el-icon-refresh-left" @click="handleUpdate(scope.row)">刷新
                  </el-button>
                  <el-button v-hasRole="['admin']" size="mini" type="text" icon="el-icon-sell" @click="handleSettlement(scope.row)">结算
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize" @pagination="getList" />
          </el-col>
        </pane>
      </splitpanes>
    </el-row>
  </div>
</template>

<script>
import {
  settlementList,
  updateSettlement,
  settlement
} from "@/api/settleAccounts/index";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { Splitpanes, Pane } from "splitpanes";
import "splitpanes/dist/splitpanes.css";

export default {
  name: "User",
  dicts: ["user_type", "sys_normal_disable", "sys_user_sex"],
  components: { Splitpanes, Pane },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 默认密码
      initPassword: undefined,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phoneNumber: undefined,
        orderByColumn: 'createTime',
        isAsc: 'descending',
      }
    };
  },
  watch: {
  },
  created() {
    this.getList();
    this.getConfigKey("sys.user.initPassword").then((response) => {
      this.initPassword = response.msg;
    });
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      settlementList(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        phone: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 刷新数据库数据
    handleUpdate(row) {
      updateSettlement({
        userId: row.userId
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("数据刷新成功");
      })
    },
    /** 结算按钮操作 */
    handleSettlement(row) {
      this.$modal
        .confirm('是否确认结算？')
        .then(function () {
          return settlement({ userId: row.userId });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("结算成功");
        })
        .catch(() => { });
    },
  },
};
</script>
